/**
 * 
 */
package cn.com.pansky.util;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import cn.com.pansky.payment.PayConf;

/**
 * 工具类
 * @author 清算平台项目组
 * @version v1.0.2
 * @date 2013-04-17
 */
public class PayUtils {
	
	/**
	 * 签名串计算String sign = PayUtils.signDictionary(paramMap, paramKeys,
				reqMap.get("signKey"), "UTF-8");
	 * @param paramVal 参与签名的参数值
	 * @param paramKeys 参与签名的参数
	 * @return 签名结果
	 */
	public static String signDictionary(Map<String, String> paramVal,String[] paramKeys,String signKey,String charset) {
		String charsetplus = StringUtils.isBlank(charset) ? PayConf.CHARSET : charset;
		StringBuffer envsrc = new StringBuffer(StringUtils.EMPTY);
		for (int i = 0; i < paramKeys.length; i++) {
			envsrc.append("&").append(paramKeys[i]).append("=").append(paramVal.get(paramKeys[i]));
		}
		envsrc.append("&").append(Crypto.GetMessageDigest(PayConf.SIGNKEY, "MD5",charsetplus));
		return Crypto.GetMessageDigest(envsrc.toString().substring(1), "MD5",charsetplus);// 返回加密后的字符串
	}
	
	/**
     * 元转换为分
     * 
     * @param amount
     * @return int
     */
    public static int convertAmountY2F(Double amount) {
        double orderAmountSrc = Arith.mul(amount, new Double("100"));
        int orderAmount = (int) orderAmountSrc;
        return orderAmount;
    }
    
    public static void main(String[] args) {
    	String src = "version=1.0.0&charset=UTF-8&signMethod=MD5&transType=02&merId=10001&backEndUrl=http://192.168.2.4:1123/jspt_example/recvNotify.action&orderTime=20131206153940&orderNumber=20131206111133001&qid=J010012014022614385487310001&refAmount=20000&orderCurrency=156&refInfo=333333333333%7C%E5%BB%BA%E8%A1%8C%7Cabc%7C11111111111%7Cbdbdbdbdbdb&merReserved1=&041902fb6f1e630ffb88d392348ec36e";
    	String sign = Crypto.GetMessageDigest(src, "MD5", "UTF-8");
    	System.out.println("sign=" + sign);
    }
}
